import Vue from 'vue'
import VueRouter from 'vue-router'
import echarts from 'echarts';//引入echarts
import {getToken} from "@/utils/storage.js";
Vue.prototype.$echarts = echarts;//引入echarts
import Login from "@/views/Login.vue";
import Admin from "@/views/Admin.vue";
import ReportList from "@/views/ReportList.vue";

import EmployeeList from "@/views/EmployeeList.vue";
import SetmealList from "@/views/SetmealList.vue";
import CheckList from "@/views/CheckList.vue";
import CategoryList from "@/views/CategoryList.vue";
import AdminLayout from "@/views/AdminLayout.vue";
import OrderList from "@/views/OrderList.vue";
import Cookies from 'js-cookie'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.prototype.$echarts = echarts;//引入echarts


Vue.use(VueRouter)

const routes = [
  {
    path: "*",
    redirect:"/login"

  },
  {
    path: "/login",
    //redirect: "/login",//设置默认跳转路径
    component: Login
  },
  {
    path: "/admin",
    component: Admin,
    meta: {
      requireAuth: true,  // 该路由项需要权限校验
    },
    children:[
      {
        path: "/adminLayout",
        component: AdminLayout,
        meta: {
          requireAuth: true,  // 该路由项需要权限校验
        },
      },

      {
        path: "/reportList",
        component: ReportList,
        meta: {
          requireAuth: true,  // 该路由项需要权限校验
        },
      },
      {
        path: "/employeeList",
        component: EmployeeList,
        meta: {
          requireAuth: true,  // 该路由项需要权限校验
        },
      },
      {
        path: "/setmealList",
        component: SetmealList,
        meta: {
          requireAuth: true,  // 该路由项需要权限校验
        },
      },
      {
        path: "/checkList",
        component: CheckList,
        meta: {
          requireAuth: true,  // 该路由项需要权限校验
        },

      },
      {
        path: "/orderList",
        component: OrderList,
        meta: {
          requireAuth: true,  // 该路由项需要权限校验
        },

      },
      {
        path: "/categoryList",
        component: CategoryList,
        meta: {
          requireAuth: true,  // 该路由项需要权限校验
        },
      },
    ]
  }
];


const router = new VueRouter({
  routes
})

//没登录的情况下，访问任何一个页面都会返回登录页面
router.beforeEach((to, from, next) => {
  const token = getToken();
  if (token !== null) {
    //直接放行
    next();
  }else {
    if (to.meta.requireAuth) {
      next("/login");
    }else {
      next();
    }
  }
});
export default router;
